package org.wmq.organizationmanagement.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.wmq.organizationmanagement.common.Result;
import org.wmq.organizationmanagement.model.dto.OrganizationMemberDTO;
import org.wmq.organizationmanagement.model.po.OrganizationMaster;
import org.wmq.organizationmanagement.model.vo.MemberVO;
import org.wmq.organizationmanagement.service.OrganizationMasterService;

import java.util.List;

@RestController
@RequestMapping("organization-master")
@RequiredArgsConstructor
@Slf4j
public class OrganizationMasterController {
    private final OrganizationMasterService organizationMasterService;

    @GetMapping("/{organizationId}")
    public Result<List<MemberVO>> getMasterLisByOrganizationId(@PathVariable Long organizationId){
        List<MemberVO> resultList = this.organizationMasterService.getListByOrganizationId(organizationId);
        if (resultList == null || resultList.isEmpty()){
            log.error("未查到任何负责人");
            return Result.error("未查到任何负责人");
        }
        log.info("查询到负责人：{}", resultList);
        return Result.ok(resultList);
    }

    @GetMapping("/{organizationId}/{masterName}")
    public Result<List<MemberVO>> getMasterLisByOrganizationIdAndMasterName(@PathVariable Long organizationId, @PathVariable String masterName){
        List<MemberVO> resultList = this.organizationMasterService.getListByOrganizationIdAndMasterName(organizationId, masterName);
        if (resultList == null || resultList.isEmpty()){
            log.error("未查到任何同名负责人");
            return Result.error("未查到同名任何负责人");
        }
        log.info("查询到同名负责人：{}", resultList);
        return Result.ok(resultList);
    }

    @PostMapping
    public Result<Void> addOrganizationMaster(@RequestBody OrganizationMemberDTO organizationMemberDTO){
        OrganizationMaster organizationMaster = new OrganizationMaster();
        BeanUtils.copyProperties(organizationMemberDTO,organizationMaster);
        boolean result = this.organizationMasterService.save(organizationMaster);
        if (!result){
            log.error("添加负责人失败");
            return Result.error("添加负责人失败");
        }
        log.info("添加负责人成功");
        return Result.ok();
    }

    @DeleteMapping("/{organizationId}")
    public Result<Void> deleteByOrganizationId(@PathVariable Long organizationId){
        boolean result = this.organizationMasterService.removeByOrganizationId(organizationId);
        if (!result){
            log.error("删除负责人失败");
            return Result.error("删除负责人失败");
        }
        log.info("删除负责人成功");
        return Result.ok();
    }
    @DeleteMapping("/{organizationId}/{masterId}")
    public Result<Void> deleteByOrganizationIdAndMasterId(@PathVariable Long organizationId, @PathVariable Long masterId){
        boolean result = this.organizationMasterService.removeByOrganizationIdAndMasterId(organizationId,masterId);
        if (!result){
            log.error("删除负责人失败");
            return Result.error("删除负责人失败");
        }
        log.info("删除负责人成功");
        return Result.ok();
    }

    @DeleteMapping("/deleteByMasterId/{masterId}")
    public Result<Void> deleteByMasterId(@PathVariable Long masterId){
        boolean result = this.organizationMasterService.removeByMasterId(masterId);
        if (!result){
            log.error("删除负责人失败");
            return Result.error("删除负责人失败");
        }
        log.info("删除负责人成功");
        return Result.ok();
    }
}
